---------------------------------------------------------------------------------------------------------------
      name:  <unnamed>
       log:  wk04 NLLS Answers.log
  log type:  text
 opened on:  15 Feb 2026, 06:24:53

. 
. *Pythagorean Theorem in baseball
. 
. use "teams.dta", clear

. 
. drop if yearid < 2001
(2,864 observations deleted)

. gen y = ln(w/l)

. gen x = ln(r/ra)

. 
. reg y x

      Source |       SS           df       MS      Number of obs   =       750
-------------+----------------------------------   F(1, 748)       =   5727.84
       Model |  63.5407147         1  63.5407147   Prob > F        =    0.0000
    Residual |  8.29779102       748  .011093304   R-squared       =    0.8845
-------------+----------------------------------   Adj R-squared   =    0.8843
       Total |  71.8385058       749  .095912558   Root MSE        =    .10532

------------------------------------------------------------------------------
           y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
           x |   1.819446   .0240405    75.68   0.000     1.772251    1.866641
       _cons |  -.0028776    .003846    -0.75   0.455    -.0104279    .0046727
------------------------------------------------------------------------------

. predict what1
(option xb assumed; fitted values)

. 
. ***generate predicted w/l
. replace what1 = exp(what1)
(750 real changes made)

. 
. gen wprcnt = w/(w+l)

. 
. nl (wprcnt = r^{b0}/(r^{b0}+ra^{b0}))
(obs = 750)

Iteration 0:  residual SS =  .4947118
Iteration 1:  residual SS =  .4933807
Iteration 2:  residual SS =  .4933807
Iteration 3:  residual SS =  .4933807


      Source |      SS            df       MS
-------------+----------------------------------    Number of obs =        750
       Model |  191.28913          1  191.289131    R-squared     =     0.9974
    Residual |  .49338065        749  .000658719    Adj R-squared =     0.9974
-------------+----------------------------------    Root MSE      =   .0256655
       Total |  191.78251        750  .255710015    Res. dev.     =  -3366.503

------------------------------------------------------------------------------
      wprcnt |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         /b0 |   1.814026   .0247967    73.16   0.000     1.765347    1.862706
------------------------------------------------------------------------------

. predict what2
(option yhat assumed; fitted values)

. 
. ***generate predicted w/l
. replace what2 = what2/(1-what2)
(750 real changes made)

.  
. scatter what* x

. graph export "ex04 pythag thm.png", replace
(file ex04 pythag thm.png written in PNG format)

. 
. *NFL Field Goal tri3s... and altitude effects
. 
. use "nfl 01 24 field goal trys.dta", clear

. 
. reg fgyes dist

      Source |       SS           df       MS      Number of obs   =    24,417
-------------+----------------------------------   F(1, 24415)     =   3055.60
       Model |  384.181114         1  384.181114   Prob > F        =    0.0000
    Residual |  3069.70315    24,415  .125730213   R-squared       =    0.1112
-------------+----------------------------------   Adj R-squared   =    0.1112
       Total |  3453.88426    24,416  .141459873   Root MSE        =    .35458

------------------------------------------------------------------------------
       fgyes |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        dist |  -.0122775   .0002221   -55.28   0.000    -.0127129   -.0118422
       _cons |   1.289865   .0086325   149.42   0.000     1.272945    1.306786
------------------------------------------------------------------------------

. 
. nl (fgyes = {b0}+{b1}*dist*(1-{a}*alt))
(obs = 24,417)

Iteration 0:  residual SS =  3069.703
Iteration 1:  residual SS =  3068.902
Iteration 2:  residual SS =  3068.902
Iteration 3:  residual SS =  3068.902


      Source |      SS            df       MS
-------------+----------------------------------    Number of obs =     24,417
       Model |  384.98237          2  192.491186    R-squared     =     0.1115
    Residual |  3068.9019      24414  .125702543    Adj R-squared =     0.1114
-------------+----------------------------------    Root MSE      =   .3545455
       Total |  3453.8843      24416  .141459873    Res. dev.     =   18652.57

------------------------------------------------------------------------------
       fgyes |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         /b0 |   1.290368   .0086339   149.45   0.000     1.273445    1.307291
         /b1 |  -.0123183   .0002227   -55.32   0.000    -.0127547   -.0118818
          /a |   .0646875   .0255635     2.53   0.011     .0145815    .1147935
------------------------------------------------------------------------------
  Parameter b0 taken as constant term in model & ANOVA table

. test _b[/a] = .10

 ( 1)  [a]_cons = .1

       F(  1, 24414) =    1.91
            Prob > F =    0.1672

. 
. nl (fgyes = {b0}+ ///
> {b1}*(dist*(1-{a}*alt))^1+ ///
> {b2}*(dist*(1-{a}*alt))^2+ ///
> {b3}*(dist*(1-{a}*alt))^3+ ///
> {b4}*(dist*(1-{a}*alt))^4+ ///
> {b5}*(dist*(1-{a}*alt))^5)
(obs = 24,417)

Iteration 0:  residual SS =   3049.32
Iteration 1:  residual SS =  3048.289
Iteration 2:  residual SS =  3048.276
Iteration 3:  residual SS =  3048.276
Iteration 4:  residual SS =  3048.276
Iteration 5:  residual SS =  3048.276
Iteration 6:  residual SS =  3048.276
Iteration 7:  residual SS =  3048.276
Iteration 8:  residual SS =  3048.276
Iteration 9:  residual SS =  3048.276
Iteration 10:  residual SS =  3048.276


      Source |      SS            df       MS
-------------+----------------------------------    Number of obs =     24,417
       Model |  405.60803          6  67.6013375    R-squared     =     0.1174
    Residual |  3048.2762      24410  .124878174    Adj R-squared =     0.1172
-------------+----------------------------------    Root MSE      =   .3533811
       Total |  3453.8843      24416  .141459873    Res. dev.     =   18487.91

------------------------------------------------------------------------------
       fgyes |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         /b0 |   .8757854   .7539732     1.16   0.245    -.6020481    2.353619
         /b1 |   .0022063   .1028412     0.02   0.983    -.1993687    .2037814
          /a |   .0552899    .020166     2.74   0.006     .0157632    .0948165
         /b2 |   .0009546   .0054054     0.18   0.860    -.0096403    .0115496
         /b3 |  -.0000585   .0001372    -0.43   0.670    -.0003274    .0002103
         /b4 |   1.10e-06   1.68e-06     0.65   0.515    -2.20e-06    4.40e-06
         /b5 |  -7.19e-09   8.03e-09    -0.90   0.370    -2.29e-08    8.54e-09
------------------------------------------------------------------------------
  Parameter b0 taken as constant term in model & ANOVA table

. 
. *** Covid cases in Italy and Spain
. 
. import delimited "owid-covid-data (3).csv", clear 
(40 vars, 42,225 obs)

. 
. gen cases = total_cases
(575 missing values generated)

. 
. keep if location=="Italy" | location =="Spain"
(41,722 observations deleted)

. 
. gen over100 = (cases >= 100 & cases != .)

. sort location date

. by location: gen day = sum(over100)

. drop if day < 1
(117 observations deleted)

. 
. *scatter cases day if location == "Italy"
. 
. nl (cases = {N}/(1+exp(-({b0}+{b1}*day)))) if location=="Italy" & day <= 30
(obs = 30)

Iteration 0:  residual SS =  1.10e+10
Iteration 1:  residual SS =  3.27e+09
Iteration 2:  residual SS =  2.60e+09
Iteration 3:  residual SS =  2.23e+09
Iteration 4:  residual SS =  2.06e+09
Iteration 5:  residual SS =  3.51e+07
Iteration 6:  residual SS =   6634718
Iteration 7:  residual SS =   6615324
Iteration 8:  residual SS =   6614983
Iteration 9:  residual SS =   6614983
Iteration 10:  residual SS =   6614983


      Source |      SS            df       MS
-------------+----------------------------------    Number of obs =         30
       Model |  1.945e+10          3  6.4834e+09    R-squared     =     0.9997
    Residual |  6614982.8         27  244999.365    Adj R-squared =     0.9996
-------------+----------------------------------    Root MSE      =   494.9741
       Total |  1.946e+10         30   648556649    Res. dev.     =   454.2458

------------------------------------------------------------------------------
       cases |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          /N |   118346.4   5184.529    22.83   0.000     107708.7    128984.2
         /b0 |  -5.651827   .0433027  -130.52   0.000    -5.740677   -5.562977
         /b1 |   .1941505   .0038348    50.63   0.000     .1862822    .2020189
------------------------------------------------------------------------------

. predict italy30
(option yhat assumed; fitted values)

. nl (cases = {N}/(1+exp(-({b0}+{b1}*day)))) if location=="Italy" & day <= 60
(obs = 60)

Iteration 0:  residual SS =  2.57e+11
Iteration 1:  residual SS =  2.36e+10
Iteration 2:  residual SS =  2.29e+09
Iteration 3:  residual SS =  9.95e+08
Iteration 4:  residual SS =  9.61e+08
Iteration 5:  residual SS =  9.61e+08
Iteration 6:  residual SS =  9.61e+08
Iteration 7:  residual SS =  9.61e+08


      Source |      SS            df       MS
-------------+----------------------------------    Number of obs =         60
       Model |  6.020e+11          3  2.0068e+11    R-squared     =     0.9984
    Residual |  9.614e+08         57  16866221.2    Adj R-squared =     0.9983
-------------+----------------------------------    Root MSE      =   4106.851
       Total |  6.030e+11         60  1.0050e+10    Res. dev.     =   1165.644

------------------------------------------------------------------------------
       cases |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          /N |   187004.9   2413.275    77.49   0.000     182172.4    191837.4
         /b0 |   -4.58696   .1029063   -44.57   0.000    -4.793027   -4.380894
         /b1 |   .1270643   .0035095    36.21   0.000     .1200366     .134092
------------------------------------------------------------------------------

. predict italy60
(option yhat assumed; fitted values)

. nl (cases = {N}/(1+exp(-({b0}+{b1}*day)))) if location=="Italy" & day <= 90
(obs = 90)

Iteration 0:  residual SS =  6.42e+11
Iteration 1:  residual SS =  3.81e+10
Iteration 2:  residual SS =  2.15e+10
Iteration 3:  residual SS =  3.82e+09
Iteration 4:  residual SS =  3.36e+09
Iteration 5:  residual SS =  3.36e+09
Iteration 6:  residual SS =  3.36e+09
Iteration 7:  residual SS =  3.36e+09
Iteration 8:  residual SS =  3.36e+09


      Source |      SS            df       MS
-------------+----------------------------------    Number of obs =         90
       Model |  1.977e+12          3  6.5913e+11    R-squared     =     0.9983
    Residual |  3.356e+09         87  38573318.4    Adj R-squared =     0.9982
-------------+----------------------------------    Root MSE      =   6210.742
       Total |  1.981e+12         90  2.2008e+10    Res. dev.     =   1824.484

------------------------------------------------------------------------------
       cases |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          /N |   222896.1   1613.402   138.15   0.000     219689.3    226102.9
         /b0 |  -3.958191   .0880043   -44.98   0.000    -4.133109   -3.783273
         /b1 |   .0974965    .002396    40.69   0.000     .0927342    .1022589
------------------------------------------------------------------------------

. predict italy90
(option yhat assumed; fitted values)

. nl (cases = {N}/(1+exp(-({b0}+{b1}*day)))) if location=="Italy" & day <= 120
(obs = 120)

Iteration 0:  residual SS =  9.28e+11
Iteration 1:  residual SS =  1.06e+11
Iteration 2:  residual SS =  8.65e+10
Iteration 3:  residual SS =  5.42e+10
Iteration 4:  residual SS =  6.69e+09
Iteration 5:  residual SS =  4.89e+09
Iteration 6:  residual SS =  4.88e+09
Iteration 7:  residual SS =  4.88e+09
Iteration 8:  residual SS =  4.88e+09
Iteration 9:  residual SS =  4.88e+09
Iteration 10:  residual SS =  4.88e+09


      Source |      SS            df       MS
-------------+----------------------------------    Number of obs =        120
       Model |  3.627e+12          3  1.2090e+12    R-squared     =     0.9987
    Residual |  4.884e+09        117  41742203.5    Adj R-squared =     0.9986
-------------+----------------------------------    Root MSE      =   6460.821
       Total |  3.632e+12        120  3.0267e+10    Res. dev.     =    2443.15

------------------------------------------------------------------------------
       cases |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          /N |   231970.5   1049.426   221.05   0.000     229892.2    234048.9
         /b0 |  -3.756723   .0761259   -49.35   0.000    -3.907486   -3.605959
         /b1 |    .089562   .0018847    47.52   0.000     .0858294    .0932945
------------------------------------------------------------------------------

. predict italy120
(option yhat assumed; fitted values)

. 
. scatter cases italy* day if location=="Italy"

. graph export "ex04 Italy.png", replace
(file ex04 Italy.png written in PNG format)

. 
. 
. *scatter cases day if location == "Spain"
. 
. nl (cases = {N}/(1+exp(-({b0}+{b1}*day)))) if location=="Spain" & day <= 30
(obs = 30)

Iteration 0:  residual SS =  3.25e+10
Iteration 1:  residual SS =  9.19e+09
Iteration 2:  residual SS =  6.97e+09
Iteration 3:  residual SS =  5.19e+09
Iteration 4:  residual SS =  3.81e+09
Iteration 5:  residual SS =  2.19e+09
Iteration 6:  residual SS =  2.57e+07
Iteration 7:  residual SS =  1.94e+07
Iteration 8:  residual SS =  1.94e+07
Iteration 9:  residual SS =  1.94e+07
Iteration 10:  residual SS =  1.94e+07


      Source |      SS            df       MS
-------------+----------------------------------    Number of obs =         30
       Model |  5.721e+10          3  1.9071e+10    R-squared     =     0.9997
    Residual |   19353816         27  716808.003    Adj R-squared =     0.9996
-------------+----------------------------------    Root MSE      =   846.6451
       Total |  5.723e+10         30  1.9077e+09    Res. dev.     =   486.4524

------------------------------------------------------------------------------
       cases |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          /N |     140599   3149.829    44.64   0.000     134136.1    147061.9
         /b0 |  -5.922982   .0688445   -86.03   0.000    -6.064239   -5.781724
         /b1 |   .2323225   .0043748    53.10   0.000     .2233462    .2412988
------------------------------------------------------------------------------

. predict spain30
(option yhat assumed; fitted values)

. nl (cases = {N}/(1+exp(-({b0}+{b1}*day)))) if location=="Spain" & day <= 60
(obs = 60)

Iteration 0:  residual SS =  3.90e+11
Iteration 1:  residual SS =  2.63e+10
Iteration 2:  residual SS =  2.82e+09
Iteration 3:  residual SS =  1.31e+09
Iteration 4:  residual SS =  1.31e+09
Iteration 5:  residual SS =  1.31e+09
Iteration 6:  residual SS =  1.31e+09
Iteration 7:  residual SS =  1.31e+09
Iteration 8:  residual SS =  1.31e+09


      Source |      SS            df       MS
-------------+----------------------------------    Number of obs =         60
       Model |  1.024e+12          3  3.4125e+11    R-squared     =     0.9987
    Residual |  1.309e+09         57  22956634.4    Adj R-squared =     0.9987
-------------+----------------------------------    Root MSE      =   4791.308
       Total |  1.025e+12         60  1.7084e+10    Res. dev.     =   1184.142

------------------------------------------------------------------------------
       cases |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          /N |   209506.4   1713.234   122.29   0.000     206075.7    212937.1
         /b0 |   -4.66775   .1081392   -43.16   0.000    -4.884295   -4.451205
         /b1 |   .1518767   .0039118    38.83   0.000     .1440435      .15971
------------------------------------------------------------------------------

. predict spain60
(option yhat assumed; fitted values)

. nl (cases = {N}/(1+exp(-({b0}+{b1}*day)))) if location=="Spain" & day <= 90
(obs = 90)

Iteration 0:  residual SS =  7.11e+11
Iteration 1:  residual SS =  7.77e+10
Iteration 2:  residual SS =  6.19e+10
Iteration 3:  residual SS =  3.77e+10
Iteration 4:  residual SS =  5.93e+09
Iteration 5:  residual SS =  4.24e+09
Iteration 6:  residual SS =  4.23e+09
Iteration 7:  residual SS =  4.23e+09
Iteration 8:  residual SS =  4.23e+09
Iteration 9:  residual SS =  4.23e+09
Iteration 10:  residual SS =  4.23e+09


      Source |      SS            df       MS
-------------+----------------------------------    Number of obs =         90
       Model |  2.598e+12          3  8.6605e+11    R-squared     =     0.9984
    Residual |  4.234e+09         87  48669932.3    Adj R-squared =     0.9983
-------------+----------------------------------    Root MSE      =   6976.384
       Total |  2.602e+12         90  2.8915e+10    Res. dev.     =   1845.409

------------------------------------------------------------------------------
       cases |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          /N |   228092.2   1294.595   176.19   0.000       225519    230665.3
         /b0 |  -4.132383   .1080484   -38.25   0.000    -4.347141   -3.917625
         /b1 |   .1265061    .003424    36.95   0.000     .1197005    .1333116
------------------------------------------------------------------------------

. predict spain90
(option yhat assumed; fitted values)

. nl (cases = {N}/(1+exp(-({b0}+{b1}*day)))) if location=="Spain" & day <= 120
(obs = 120)

Iteration 0:  residual SS =  9.32e+11
Iteration 1:  residual SS =  1.78e+11
Iteration 2:  residual SS =  1.54e+11
Iteration 3:  residual SS =  1.28e+11
Iteration 4:  residual SS =  1.24e+11
Iteration 5:  residual SS =  8.21e+09
Iteration 6:  residual SS =  7.98e+09
Iteration 7:  residual SS =  7.97e+09
Iteration 8:  residual SS =  7.97e+09
Iteration 9:  residual SS =  7.97e+09
Iteration 10:  residual SS =  7.97e+09
Iteration 11:  residual SS =  7.97e+09
Iteration 12:  residual SS =  7.97e+09


      Source |      SS            df       MS
-------------+----------------------------------    Number of obs =        120
       Model |  4.381e+12          3  1.4605e+12    R-squared     =     0.9982
    Residual |  7.968e+09        117  68106660.4    Adj R-squared =     0.9981
-------------+----------------------------------    Root MSE      =   8252.676
       Total |  4.389e+12        120  3.6578e+10    Res. dev.     =   2501.897

------------------------------------------------------------------------------
       cases |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          /N |   237016.5   1117.363   212.12   0.000     234803.6    239229.3
         /b0 |  -3.851879   .1069931   -36.00   0.000    -4.063773   -3.639984
         /b1 |   .1141461    .003191    35.77   0.000     .1078265    .1204657
------------------------------------------------------------------------------

. predict spain120
(option yhat assumed; fitted values)

. 
. scatter cases spain* day if location=="Spain"

. graph export "ex04 spain.png", replace
(file ex04 spain.png written in PNG format)

. 
. log close
      name:  <unnamed>
       log:  wk04 NLLS Answers.log
  log type:  text
 closed on:  15 Feb 2026, 06:25:06
---------------------------------------------------------------------------------------------------------------
